﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Drawing.Imaging;
using System.IO;
using System.Timers;
using Common;
using DotNetCSharp;
namespace SmartLauncher
{
    public partial class LauncherForm : Form
    {
        const string POKER_APP_PATH = "C:\\Program Files\\PokerStars\\PokerStars.exe";
        Rectangle _loginButtonRect = new Rectangle(new Point(443, 384), new Size(111, 82));
        Rectangle _registerButtonRect = new Rectangle(new Point(647, 24), new Size(122, 39));
        Rectangle _buyInButtonRect = new Rectangle(new Point(190, 149), new Size(132, 22));
        Rectangle _resultWindowNoButtonRect = new Rectangle(new Point(312, 129), new Size(71, 19));
        Rectangle firstTableRect = new Rectangle(new Point(288, 293), new Size(480, 26));
        Point _lobbyLocation = new Point(342, 540);
        AppWindow loginWindow, lobbyWindow, myNewsWindow;
        AppWindow tableWindow, _buyInWindow, registerWindow, resultWindow, registeredWindow;
        Dictionary<IntPtr,Process> tableWorkerDic = new Dictionary<IntPtr, Process>();
        int _numberTables, _numPlayedTables, _availableTable;
        public LauncherForm()
        {
            InitializeComponent();
            btnStop.Enabled = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            _numberTables = Int32.Parse(txtNumTable.Text);
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            try
            {
                Process pokerApp = new Process();
                pokerApp.StartInfo.FileName = POKER_APP_PATH;
                if (pokerApp.Start())
                {
                    while (!IsLobbyScreen())
                    {
                        Thread.Sleep(2000);
                    }
                    lobbyWindow.Restore(new Rectangle(820, 250, 1620, 842));
                    Thread.Sleep(2000);
                    ClickButton(lobbyWindow, new Point(719, 538));
                    while (!IsLoginScreen())
                    {
                        Thread.Sleep(2000);
                    }
                    ClickButton(loginWindow, new Point(113, 203));
                    Thread.Sleep(5000);
                    if (IsMyNewsScreen())
                    {
                        myNewsWindow.Close();
                        Thread.Sleep(5000);
                    }
                    if (_numberTables == 1)
                    {
                        StartNewTable(0);
                        Thread.Sleep(2000);
                        _numPlayedTables = 1;
                    }
                    else
                    {
                        for (int i = 0; i < 2; i++)
                        {
                            StartNewTable(i);
                            Thread.Sleep(2000);
                        }
                        _numPlayedTables = 2;
                        lblStatus.Text = "Playing:" + _numPlayedTables.ToString();
                    }
                    NativeApi.MinimizeWindow(lobbyWindow.Hwnd);
                    tmrPoolTables.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Can't start poker program");
                }

            }
            catch (Exception)
            {
                MessageBox.Show("Error starting poker program");
            }

        }
        bool RegisterGame()
        {
            lobbyWindow.Restore(new Rectangle(820, 250, 1620, 842));
            DoubleClickButton(lobbyWindow, new Point(223, 283));

            while (!IsRegisterScreen())
            {
                Thread.Sleep(2000);
            }
            ClickButton(registerWindow, new Point(150,496));
            for (int i = 0; i < 3; i++)
            {
                if(IstBuyInScreen())
                {
                    break;
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }
            if (_buyInWindow != null)
            {
                ClickButton(_buyInWindow, new Point(109, 293));
                Thread.Sleep(2000);
                return true;
            }
            else
            {
                registerWindow.Close();
                return false;
            }
        }
        void StartNewTable(int tableNumber)
        {
            while (!RegisterGame())
            {
                Thread.Sleep(2000);
            }
            Thread.Sleep(2000);
            if (IsRegisteredScreen())
            {
                registeredWindow.Close();
            }            
            while (!IsTableScreen())
            {
                Thread.Sleep(1000);
            }
            registerWindow.Close();
            Thread.Sleep(5000);
            tableWindow.Location = new Point(tableNumber * 820, 0);
            tableWindow.BringWindowToFront();
            Thread.Sleep(5000);
            int tourney = tableWindow.Title.IndexOf("Tournament");
            int table = tableWindow.Title.IndexOf("Table");
            string tourneyNum = tableWindow.Title.Substring(tourney + "Tournament".Length, table - tourney - "Tournament".Length).Trim();

            Process fuxi = new Process();
            fuxi.StartInfo.FileName = "DotNetCSharp.exe";
            fuxi.StartInfo.Arguments = tourneyNum;
            if (fuxi.Start())
            {
                if (!tableWorkerDic.ContainsKey(tableWindow.Hwnd))
                {
                    tableWorkerDic.Add(tableWindow.Hwnd, fuxi);
                }
            }
        }

        void ClickButton(AppWindow win, Point point)
        {
            win.BringWindowToFront();
            Mouse.Click(point.X + win.Rect.X, point.Y + win.Rect.Y);
        }
        void DoubleClickButton(AppWindow win, Point point)
        {
            win.BringWindowToFront();
            Mouse.DoubleClick(point.X + win.Rect.X, point.Y + win.Rect.Y);
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            tmrPoolTables.Enabled = false;
        }

        private void tmrPoolTable_Tick(object sender, EventArgs e)
        {
            tmrPoolTables.Enabled = false;
            if (IsResultWindowDisplayed())
            {
                foreach (IntPtr hwnd in tableWorkerDic.Keys)
                {
                    AppWindow win = new AppWindow(hwnd);
                    if (resultWindow.Rect.X > win.Rect.X && resultWindow.Rect.X < win.Rect.X + win.Size.Width)
                    {
                        _availableTable = (win.Location.X == 0 ? 0 : 1);
                        Process worker = tableWorkerDic[hwnd];
                        worker.Kill();
                        Thread.Sleep(2000);
                        resultWindow.BringWindowToFront();
                        resultWindow.Close();
                        win.Close();
                        tableWorkerDic.Remove(hwnd);
                        Thread.Sleep(5000);
                        if (_numPlayedTables < _numberTables)
                        {
                            StartNewTable(_availableTable);
                            _numPlayedTables++;
                            lblStatus.Text = "Playing:" + _numPlayedTables.ToString();
                            Thread.Sleep(5000);
                        }
                        else
                        {
                            lblStatus.Text = "Finished!";
                        }
                        break;
                    }
                }
            }
            tmrPoolTables.Enabled = true;
        }
    }
}
